package com.cola.programmer.controller.admin;

import com.cola.programmer.entity.admin.User;
import com.cola.programmer.service.admin.UserService;
import com.cola.programmer.util.CpachaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Grup:com.cola.programmer.controller
 * @Auther:cola
 * @Data:2021/5/20-2021-05-23:41
 * @version:1.0
 *
 * 系统操作类控制器
 */
@Controller
@RequestMapping("/system")
public class SystemController {
    @Autowired
    private UserService userService;

    @RequestMapping(value="/index",method = RequestMethod.GET)
    public ModelAndView index(ModelAndView model){
        model.setViewName("system/index");
        model.addObject("name","cola");
        return model;
     }

    /*
     *
     * @param model
     * @return org.springframework.web.servlet.ModelAndView
     * @author cola
     * @date 2021/5/23 22:28
     * Description: 系统登录后的欢迎页面//TODO
     */
    @RequestMapping(value="/welcome",method = RequestMethod.GET)
    public ModelAndView welcome(ModelAndView model){
        model.setViewName("system/welcome");
        return model;
    }
     /*
      * TODO 
      * 
      * @param model
      * @return org.springframework.web.servlet.ModelAndView
      * @author cola
      * @date 2021/5/22 14:48
      * Description: //TODO 
      */
     @RequestMapping(value = "/login",method = RequestMethod.GET)
     public ModelAndView login(ModelAndView model){
        model.setViewName("system/login");
        return model;
     }

    /*
     * TODO
     *
     * @param user
     * @param cpacha
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author cola
     * @date 2021/5/22 21:48
     * Description: //TODO 登录表单提交处理控制器
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
     public Map<String,String > loginAct(User user,String cpacha,HttpServletRequest request){
         Map<String,String>ret=new HashMap<String, String>();
        if (user==null){
            ret.put("type","error");
            ret.put("msg","请填写用户信息!");
            return ret;
        }
        if (StringUtils.isEmpty(cpacha)){
            ret.put("type","error");
            ret.put("msg","请填写验证码!");
            return ret;
        }
        if (StringUtils.isEmpty(user.getUsername())){
            ret.put("type","error");
            ret.put("msg","请填写用户名!");
            return ret;
        }
        if (StringUtils.isEmpty(user.getPassword())){
            ret.put("type","error");
            ret.put("msg","请填写密码!");
            return ret;
        }
        Object loginCpacha = request.getSession().getAttribute("loginCpacha");
        if (loginCpacha==null){
            ret.put("type","error");
            ret.put("msg","会话超时,请刷新页面!!");
            return ret;
        }
        User byUsername = userService.findByUsername(user.getUsername());
        if (byUsername==null){
            ret.put("type","error");
            ret.put("msg","该用户名不存在!!");
            return ret;
        }
        if (!user.getPassword().equals(byUsername.getPassword())){
            ret.put("type","error");
            ret.put("msg","密码错误!!");
            return ret;
        }
        if (!cpacha.toUpperCase().equals(loginCpacha.toString().toUpperCase())){
            ret.put("type","error");
            ret.put("msg","验证码错误!!");
            return ret;
        }
        request.getSession().setAttribute("admin",byUsername);
        ret.put("type","success");
        ret.put("msg","登录成功!");
        return ret;
     }

     /*
      * TODO 本系统所有的验证码都采用此方法
      *
      * @param vcodeLen
      * @param width
      * @param height
      * @param cpacharType:用来区别验证码的类型,传入字符串
      * @param request
      * @param response
      * @return void
      * @author cola
      * @date 2021/5/22 14:53
      * Description: //TODO
      */
     @RequestMapping(value = "/get_cpacha",method = RequestMethod.GET)
     public void generateCpacha(
             @RequestParam(name = "vl",required = false,defaultValue = "4") Integer vcodeLen,
             @RequestParam(name = "w",required = false,defaultValue = "100") Integer width,
             @RequestParam(name = "h",required = false,defaultValue = "30") Integer height,
             @RequestParam(name = "type",required = true,defaultValue = "loginCpacha") String cpachaType,
             HttpServletRequest request,
             HttpServletResponse response){
         CpachaUtil cpachaUtil = new CpachaUtil(vcodeLen, width, height);
         String generatorVCode = cpachaUtil.generatorVCode();
         request.getSession().setAttribute(cpachaType,generatorVCode);
         request.getSession().setMaxInactiveInterval(60*60);
         BufferedImage generatorRotateVCodeImage = cpachaUtil.generatorRotateVCodeImage(generatorVCode, true);
         try {
             ImageIO.write(generatorRotateVCodeImage,"gif",response.getOutputStream());
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

}
